package com.yang.designmode.proxy.business;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class StartApp {
    public static void main(String[] args) {
        UserController userController = new UserController();

        UserController proxy = new MethodInterceptor() {
            public UserController createProxy(Class targetClass) {
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(targetClass);
                enhancer.setCallback(this);
                return (UserController) enhancer.create();
            }

            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                long start = System.currentTimeMillis();
                Object res = proxy.invoke(userController, args);
                long end = System.currentTimeMillis();
                System.out.println("耗时：" + (end - start));
                if(end-start>500){
                    System.out.println("耗时过长");
                }
                return res;
        }
        }.createProxy(UserController.class);

        proxy.login();
    }
}




